package com.example.RemindMe;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class FindMyCarActivity extends Activity implements LocationListener {

	
	private LocationManager myManager;
	Button btn;
	SharedPreferences position;
	Button btnStartGMaps;
	ProgressDialog progress;
	TextView address;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_findmycar);
        position = getSharedPreferences("position", 0);
        btnStartGMaps = (Button) findViewById(R.id.button2);
        
        progress = new ProgressDialog(FindMyCarActivity.this);
        progress.setTitle("save location data");
        progress.setMessage("Loading...");
        progress.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				stopListening();
				
			}
           
            
        });
        
        btn = (Button) findViewById(R.id.button1);
        address = (TextView) findViewById(R.id.textView6);
        
        
        if(position.getString("lat", "0").equals("0")) {
        	btnStartGMaps.setClickable(false);
        }
        
        else {
        	btnStartGMaps.setClickable(true);


        }
        
      LocationToAddressAsync lta = new LocationToAddressAsync();
      lta.setTextView(address);
      lta.execute(position.getString("lat","0"),position.getString("longt","0"));
        
        /**
         * 
         * LocationManager
         * 
         */
        myManager = (LocationManager) getSystemService(LOCATION_SERVICE);


        
    }

    /*
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
  
    */
    /**
     * 
     * "Listener" starten um aktuelle Positionsdaten zu erhalten
     * 
     */
    @SuppressLint("NewApi")
	public void startListening(View v) {
    	
    	progress.show();
    	btn.setClickable(false);
    	btn.setText("save location ...");
        myManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            0, 
            0, 
            (LocationListener) this
        );
    }
    
    
    @SuppressLint("NewApi")
	public void stopListening() {
    	progress.dismiss();
    	btn.setClickable(true);
    	btn.setText("save location");
        if (myManager != null)
            myManager.removeUpdates((LocationListener) this);
    }

    /**
     * 
     * Verändert sich die Location wird die Gefundene in eine SharedPreference gespeichert.
     * 
     */
	public void onLocationChanged(Location location) {
		
		SharedPreferences position = getSharedPreferences("position", 0);
		SharedPreferences.Editor editor = position.edit();
		editor.putString("longt", ""+location.getLongitude());
		editor.putString("lat", ""+location.getLatitude());
		editor.commit();
		
		btnStartGMaps.setClickable(true);
		stopListening();
		
		LocationToAddressAsync lta = new LocationToAddressAsync();
	      lta.setTextView(address);
	      lta.execute(position.getString("lat","0"),position.getString("longt","0"));
		 
		
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
    
   
	/**
	 * 
	 * Mit den letztgespeicherten Daten GoogleMaps aufrufen.
	 * 
	 */
	public void getCarPosition(View v) {
		
		String longt = position.getString("longt", "0");
		String lat = position.getString("lat", "0");;
	    Uri uriUrl = Uri.parse("https://maps.google.com/?q=" + lat + "," + longt);
	    Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
	    
	    startActivity(launchBrowser);

	}

    //------------------------------------------------Action Bar Methods -----------------------------------------------
    public void onClickHome (View v)
    {
        goHome (this);
    }

    /**
     * Handle the click on the search button.
     *
     * @param v View
     * @return void
     */

    public void onClickSearch (View v)
    {
        startActivity (new Intent(getApplicationContext(), SearchActivity.class));
        finish();
    }

    /**
     * Handle the click on the About button.
     *
     * @param v View
     * @return void
     */

    public void onClickAbout (View v)
    {
        startActivity (new Intent(getApplicationContext(), AboutActivity.class));
        finish();
    }

    /**
     * Handle the click of a Feature button.
     *
     * @param v View
     * @return void
     */
    /**
     * Go back to the home activity.
     *
     * @param context Context
     * @return void
     */

    public void goHome(Context context)
    {
        final Intent intent = new Intent(context, HomeActivity.class);
        intent.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity (intent);
        finish();
    }

    /**
     * Use the activity label to set the text in the activity's title text view.
     * The argument gives the name of the view.
     *
     * <p> This method is needed because we have a custom title bar rather than the default Android title bar.
     * See the theme definitons in styles.xml.
     *
     * @param textViewId int
     * @return void
     */

    public void setTitleFromActivityLabel (int textViewId)
    {
        TextView tv = (TextView) findViewById (textViewId);
        if (tv != null) tv.setText (getTitle ());
    } // end setTitleText

    /**
     * Show a string on the screen via Toast.
     *
     * @param msg String
     * @return void
     */

    public void toast (String msg)
    {
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();
    } // end toast

    /**
     * Send a message to the debug log and display it using Toast.
     */
    public void trace (String msg)
    {
        Log.d("Demo", msg);
        toast (msg);
    }


}
